import os
import tables
from PyQt4.QtGui import QFileDialog

def open_file():
    filename = str(QFileDialog.getOpenFileName())
    if filename:
        print "Opened: ", filename
        return filename

def get_hdf5():
    hdf5_file=tables.openFile(open_file())
    return hdf5_file

def parse_node(node):
	for sub_node in node._f_iterNodes():
		if sub_node._c_classId is 'GROUP':
			print '\t', sub_node, ',', type(sub_node), ',', sub_node._c_classId, '\n'
			parse_node(sub_node)
		else:
			print '\t', sub_node, ',', type(sub_node), ',', sub_node._c_classId
		

def parse_hdf5(hdf5_file):
	for node in hdf5_file.root._f_iterNodes():
		if node._c_classId is 'GROUP':
			
			print node, ',', type(node), ',', node._c_classId, '\n'
			parse_node(node)
		else:
			#The remaining elements are then children
			print node, ',', type(node), ',', node._c_classId, '\n'
		

'''so it looks like a dictionary can be returned by using the 
_v_children command.  This may be useful in iterating through the items.
_c_classId is also usefull for determining what to do...


from string import split
from os.path import isdir,join,normpath
from os import listdir

def parsefolder(dirname):
	if not isdir(dirname): return
	file_list = sorted([
		normpath(join(dirname, fname))
		for fname in listdir(dirname)
		if fname.lower().endswith('.py')
		])
	print file_list

import imp, os.path, sys

def import_(filename, prefix):
    path, name = os.path.split(filename)
    name, ext = os.path.splitext(name)

    modname = "%s_%s" % (prefix, name)

    print 'Before: %s in sys.modules ==' % modname, modname in sys.modules
    print 'Before: %s in sys.modules ==' % name, name in sys.modules
    file, filename, data = imp.find_module(name, [path])
    mod = imp.load_module(name, file, filename, data)
    print 'After: %s in sys.modules ==' % modname, modname in sys.modules
    print 'After: %s in sys.modules ==' % name, name in sys.modules
    return mod



import os, sys

def import_libs(dir):
    """ Imports the libs, returns a list of the libraries. 
	Pass in dir to scan """
    
    library_list = [] 
    
    for f in os.listdir(os.path.abspath(dir)):       
        module_name, ext = os.path.splitext(f) # Handles no-extension files, etc.
        if ext == '.py': # Important, ignore .pyc/other files.
            print module_name
            str2execute = 'from %s import %s as %s' % (dir, module_name, module_name)
            print str2execute
            #library_list.append(module_name)
            exec str2execute
            #full_name = '.'.join([dir,module_name])
            #module = __import__(full_name, level = 0)
            #print 'imported module: %s' % (module_name)
            
        #return library_list




import os, sys

def import_libs(dir):
    """ Imports the libs, returns a list of the libraries. 
	Pass in dir to scan """
    
    library_list = [] 
    
    for f in os.listdir(os.path.abspath(dir)):       
        module_name, ext = os.path.splitext(f) # Handles no-extension files, etc.
        if ext == '.py': # Important, ignore .pyc/other files.
            full_name = '.'.join([dir,module_name])
            module = __import__(full_name, level = 0)
            print 'imported module: %s' % (module_name)
            library_list.append(module)
    return library_list

exec "import " + f[0]

'''
